using System;
using System.Data;

namespace Glpt.Common
{
    /// <summary>
    /// 操作权限
    /// </summary>
    [Flags]
    public enum OperateRight
    {
        /// <summary>
        /// ReadOnly
        /// </summary>
        ReadOnly = 0,
        /// <summary>
        /// Insert
        /// </summary>
        Insert = 1,
        /// <summary>
        /// Update
        /// </summary>
        Update = 2,
        /// <summary>
        /// Update_Insert
        /// </summary>
        Update_Insert = 3,
        /// <summary>
        /// Delete
        /// </summary>
        Delete = 4,
        /// <summary>
        /// Delete_Insert
        /// </summary>
        Delete_Insert = 5,
        /// <summary>
        /// Delete_Update
        /// </summary>
        Delete_Update = 6,
        /// <summary>
        /// All
        /// </summary>
        All = 7,
        /// <summary>
        /// ReadOnlyNotExport
        /// </summary>
        ReadOnlyNotExport = 8,
        /// <summary>
        /// All_NotExport
        /// </summary>
        All_NotExport = 15
    }

    /// <summary>
    /// 操作权限控制类
    /// </summary>
    public class OperateRightControl
    {
        /// <summary>
        /// 操作权限值
        /// </summary>
        public OperateRight OpRight = OperateRight.ReadOnly;

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public OperateRightControl()
        {
        }

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="opt_right">操作权限</param>
        public OperateRightControl(OperateRight opt_right)
        {
            OpRight = opt_right;
        }

        /// <summary>
        /// 添加对一个控件的权限控制
        /// </summary>
        /// <param name="ctrl">控件</param>
        /// <param name="ort">控件权限属性</param>
        public void AddRightControl(object ctrl, OperateRight ort)
        {
            bool ispower = (ort & OpRight) >= ort;
            if (InvokeDynamic.SetPropertyInfo(ctrl, "ReadOnly", !ispower)) return;
            if (InvokeDynamic.SetPropertyInfo(ctrl, "Enabled", ispower)) return;
            if (InvokeDynamic.SetPropertyInfo(ctrl, "Visible", ispower)) return;
        }

        /// <summary>
        /// 添加对一个控件的权限控制
        /// </summary>
        /// <param name="ctrl">控件</param>
        public void AddRightControl(object ctrl)
        {
            AddRightControl(ctrl, GetOperateRight(ctrl));
        }

        private static DictElement[] dictList = null;
        private DictElement[] DictList
        {
            get
            {
                if (dictList == null)
                {
                    DataTable DictTable = DataDictionary.GetDictTable(1038);
                    dictList = new DictElement[DictTable.Rows.Count];
                    for (int i = 0; i < dictList.Length; i++)
                    {
                        dictList[i] = new DictElement(DictTable.Rows[i]["DICT_VALUE"].ToString(), DictTable.Rows[i]["DICT_VALUE"].ToString());
                        dictList[i].Tag = int.Parse(DictTable.Rows[i]["DICT_NAME"].ToString());
                    }
                }
                return dictList;
            }
        }

        /// <summary>
        /// 获取控件的权限属性
        /// </summary>
        /// <param name="ctrl">控件</param>
        /// <returns>控件权限属性</returns>
        public OperateRight GetOperateRight(object ctrl)
        {
            OperateRight or_p = OperateRight.ReadOnly;
            object text = InvokeDynamic.GetPropertyInfo(ctrl, "Text");
            if (text != null)
            {
                string stext = text.ToString();
                if (stext.Length > 0)
                {
                    foreach (DictElement dict in DictList)
                    {
                        if (stext.IndexOf(dict.Value) >= 0)
                        {
                            or_p = (OperateRight)dict.Tag;
                            break;
                        }
                    }
                }
            }
            return or_p;
        }

        /// <summary>
        /// 是否有插入权限
        /// </summary>
        public bool HasInsert
        {
            get
            {
                return (OpRight & OperateRight.Insert) == OperateRight.Insert;
            }
        }

        /// <summary>
        /// 是否有修改权限
        /// </summary>
        public bool HasUpdate
        {
            get
            {
                return (OpRight & OperateRight.Update) == OperateRight.Update;
            }
        }

        /// <summary>
        /// 是否有删除权限
        /// </summary>
        public bool HasDelete
        {
            get
            {
                return (OpRight & OperateRight.Delete) == OperateRight.Delete;
            }
        }
    }
}
